/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.dao;

import br.com.modelo.ReservaBean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Felipe
 */
public class ReservaDAO {
    
     private Connection connection;    
   public ReservaDAO(){
       this.connection =new ConnectionFactory().getConnection();
   } 
    
   public void adiciona(ReservaBean ra){
       String sql;
        sql = "insert into reserva values(?,?,?,?)";        
       try{
           PreparedStatement stmt = connection.prepareStatement(sql);
           stmt.setString(1,ra.getDt_inicio_reserva());
            stmt.setString(2,ra.getDt_fim_reserva());
           stmt.setString(3,ra.getNm_hospede());
           stmt.setString(4,ra.getNm_quarto());
     
            
           
            stmt.execute();
           stmt.close();
       }catch(SQLException e){
           throw new RuntimeException(e);
       }
   }
   
    public List<ReservaBean> getLista() {
		try {
			
			List<ReservaBean> listareservas= new ArrayList<ReservaBean>();
			PreparedStatement stmt = this.connection
					.prepareStatement("select * from reserva");
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				ReservaBean ta = new ReservaBean();
			
                                ta.setNm_hospede(rs.getString("NM_H"));
                                ta.setNm_quarto(rs.getString("NM_Q"));
				ta.setDt_inicio_reserva(rs.getString("DT_INICIO_RESERVA"));
                                ta.setDt_fim_reserva(rs.getString("DT_FIM_RESERVA"));
				listareservas.add(ta);
			}
			rs.close();
			stmt.close();
			return listareservas;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
    }
                
                 public void altera(ReservaBean ra) {

		String sql = "update reserva set DT_INICIO_RESERVA=?,DT_FIM_RESERVA=?,NM_Q=?"
                        + " where NM_H=?";
		PreparedStatement stmt;
		try {
			stmt = connection.prepareStatement(sql);

			
			stmt.setString(1, ra.getDt_inicio_reserva());
			stmt.setString(2, ra.getDt_fim_reserva());
                        stmt.setString(3, ra.getNm_quarto());
                        stmt.setString(4, ra.getNm_hospede());
                        
                        stmt.execute();
			stmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
   
   
   public void remove(ReservaBean ra) {
		PreparedStatement stmt;
		try {
			stmt = connection.prepareStatement("delete from reserva where NM_H=?");
			stmt.setString(1, ra.getNm_hospede());
			stmt.execute();
			stmt.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}
   


}
